package ui.window
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.system.Capabilities;
	import flash.ui.Keyboard;
	import flash.ui.Mouse;
	
	import mx.events.CloseEvent;
	import mx.events.EffectEvent;
	import mx.managers.PopUpManager;
	
	import spark.components.TitleWindow;
	import spark.effects.Scale;
	
	public class IcoTitleWindow extends TitleWindow
	{
		private var _ico:Class;
		[Bindable]private var _canClose:Boolean = true;
		
		/**
		 * 屏幕中心坐标
		 */
		public static const screenX:int = Capabilities.screenResolutionX;
		public static const screenY:int = Capabilities.screenResolutionY; 	//外面引用的时候  统一同screenY/2,弹出框出现高度为屏幕Y/3
		
		public function IcoTitleWindow()
		{
			super();
			this.styleName="icoTitleWindowSkin";
			this.addEventListener(CloseEvent.CLOSE,closeHandler,false,0,true);
			this.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown,false,0,true);
		}

		public function get ico():Class
		{
			return _ico;
		}

		public function set ico(value:Class):void
		{
			_ico = value;
		}
		
		public function get canClose():Boolean{
			return _canClose;
		}
		public function set canClose(value:Boolean):void{
			this._canClose = value;
		}
		
		/**
		 * 关闭窗体
		 */
		public function closeHandler(event:CloseEvent=null,isUseEffect:Boolean=false):void
		{
			if(isUseEffect){
				var zoom:Scale = new Scale();
				zoom.autoCenterTransform = true;
				zoom.scaleXFrom = 0.1;
				zoom.scaleXTo = 1;
				zoom.scaleYFrom = 0.1;
				zoom.scaleYTo = 1;
				zoom.addEventListener(EffectEvent.EFFECT_END,onEffectEnd);
				zoom.play([this],true);
			}else{
				PopUpManager.removePopUp(this);
			}
		}
		
		/**
		 * 效果完成时触发
		 */  
		private function onEffectEnd(event:EffectEvent):void{
			PopUpManager.removePopUp(this);
		}
		
		/**
		 * 键盘ESC按下关闭
		 */ 
		private function onKeyDown(keyboard:KeyboardEvent):void{
			switch (keyboard.keyCode){
				case Keyboard.ESCAPE:
					closeHandler(null);
					break;
			}
		}
		
		private function mouseHandle(event:FocusEvent):void{
			if(event.relatedObject ==null)
				return;
			if(this.contains(event.relatedObject as DisplayObject)){
				trace(this);
			}else{
				this.closeHandler(null);
			}
		}
		
		/**
		 * realClass:窗口实际类型
		 * parent:父UI
		 * model:是否模态
		 * titleName:窗口名称
		 * isUseEffect:是否使用特效
		 */ 
		public static function show(realClass:Class,titleName:String,parent:DisplayObject,model:Boolean=true,isUseEffect:Boolean=false,showCloseBtn:Boolean=true):*{
			
			var window:IcoTitleWindow = new realClass();
			window.title = titleName;
			window.canClose = showCloseBtn;
			PopUpManager.addPopUp(window, parent,model);
			window.x=(screenX/2 - window.width/2);
//			window.y=30;
			window.y=(screenY/2 - window.height/2 - 30);
			PopUpManager.centerPopUp(window);
			
			if(isUseEffect){
				var zoom:Scale = new Scale();
				zoom.autoCenterTransform = true;
				zoom.scaleXFrom = 0.1;
				zoom.scaleXTo = 1;
				zoom.scaleYFrom = 0.1;
				zoom.scaleYTo = 1;
				zoom.play([window]);
			}
			return window;
		}
		
		override protected function moveArea_mouseDownHandler(event:MouseEvent):void
		{
			super.moveArea_mouseDownHandler(event);
		}
		
		override protected function moveArea_mouseUpHandler(event:Event):void
		{
			new Rectangle(0, 0, this.parentApplication.width - this.width, this.parentApplication.height - this.height);
			if(this.x < 0){
				this.x = 0;
			}
			if(this.y < 0){
				this.y = 0;
			}
			
			if(this.x >this.parentApplication.width - this.width){
				this.x = this.parentApplication.width - this.width;
			}
			if(this.y >this.parentApplication.height - this.height){
				this.y = this.parentApplication.height - this.height;
			}
			super.moveArea_mouseUpHandler(event);
		}

	}
}